home *** CD-ROM | disk | FTP | other *** search
- Path: news.sover.net!news
- From: mountain@sover.net (Steve Mount)
- Newsgroups: comp.lang.c
- Subject: Re: Empty String function
- Date: 17 Mar 1996 15:02:16 GMT
- Organization: SoVerNet, Inc.
- Message-ID: <4ih9ho$680@thrush.sover.net>
- References: <1996Mar14.205741.19178@vtf.idx.com> <826901446snz@genesis.demon.co.uk>
- NNTP-Posting-Host: pm0a6.mid.sover.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <826901446snz@genesis.demon.co.uk>, fred@genesis.demon.co.uk says...
- >
- >In article <1996Mar14.205741.19178@vtf.idx.com>
- > sjjm@hawkeye.idx.com "Steve Mount" writes:
- >
- >>int zemp(char *ptxt, int len)
- >>{
- >>static int i;
- >
- >Why make i static? It would be better off as an auto variable.
-
- The compiler docs for my compiler says that this allows the compiler
- to optimize this variable - it does not need to recreate it each time
- it is used. In small functions like this, we often use the register
- modifier instead of static. Just so happens in this one I used
- static.
-
- >
- >>if (len == 0) len = strlen(ptxt); // Allow null-term strings
- >
- >If you are writing C code // is illegal syntax - C only supports /* */
- >comments. A conforming compiler is required to diagnose //
-
- With a flag, we can use this style comment. Our "style guide" says all
- new code should, for readability. The compiler is "conforming", but lets
- you override with a -qcpluscmt flag.
-
- >
- >>i = strspn(ptxt," \n\t");
- >>if (i >= len) return(-1); // -1 == EMPTY
- >>return(i);
- >>}
- >>
- >>
- >>The problem is when the buffer is not null-terminated, which
- >>happens a lot. The strspn function COULD read past the end
- >>of the buffer.... potentially WAY past it. Possibly even into
- >>memory not owned by the program.
- >
- >Right, you *must* pass strspn() a pointer to null character terminated string
- >or else the call results in undefined behaviour i.e. anything can happen.
- >There's nothing to stop strspn() calling, say, strlen() internally.
-
- Yup. I see your point. You're the first one to bring up that particular
- one. As I told the others who graciously replied to me, we've done away
- with this method and gone back in source code control to the older, less
- efficient, but legal, code.
-
- We thought about a malloc each time, but benchmarking showed this slowed the
- thing way down, and we tend to avoid mallocs whenever possible - too much
- potential for trouble. For now, the code loops through the buffer in a
- for (or is it while... I forget) loop, checking each byte.
-
- Thanks for the comments and your time.
- >-----------------------------------------
- >Lawrence Kirby | fred@genesis.demon.co.uk
- >Wilts, England | 70734.126@compuserve.com
- >-----------------------------------------
- +============================================================================+
- | Steve Mount, Software Engineer Work: sjjm@hawkeye.idx.com |
- | CIS: 73720,3404 MSN: S_Mountain Home: mountain@sover.net |
- | AOL: Mountain |
- | WWW: http://www.sover.net/~mountain/ "Fight, Win, Prevail!" |
- +============================================================================+
-
-